/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sun.login;

import com.sun.courier.CourierSystem;
import com.sun.courier.dal.LoginDAL;
import com.sun.courier.entity.Administrator;
import com.sun.courier.entity.EmployeeLogin;
import com.sun.courier.utilities.Utilities;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import static org.jdesktop.application.Application.launch;

/**
 *
 * @author HONG QUAN
 */
public class Login extends JFrame implements ActionListener {

    LoginDAL lgDal = new LoginDAL();

    JPanel pnTitle = new JPanel();
    JPanel pnCenter = new JPanel();
    JPanel pnButton = new JPanel();
    JPanel pnMain = new JPanel();

    JLabel lblTitle = new JLabel("COURIER MANAGEMENT SYSTEM");
    JLabel lblUsername = new JLabel("UserName");
    JLabel lblPassword = new JLabel("Password");
    JComboBox cbFunction = new JComboBox();
    JLabel lblFunction = new JLabel("You are");

    JTextField txtUsername = new JTextField();
    JPasswordField txtPassword = new JPasswordField();

    JButton btnLogin = new JButton("Login");
    JButton btnReset = new JButton("Reset");

    public Login() {
        registerListeners();
        createControlPanel();
    }

    private void createControlPanel() {
        try {
            for (UIManager.LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (Exception e) {
            // If Nimbus is not available, you can set the GUI to another look and feel.
        }
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        pnCenter.setBorder(BorderFactory.createEtchedBorder());
        pnCenter.setLayout(gridbag);

        pnTitle.setLayout(new FlowLayout());
        pnTitle.setBorder(BorderFactory.createEtchedBorder());
        lblTitle.setFont(new Font("Arial", Font.BOLD, 25));
        pnTitle.add(lblTitle);

        c.gridx = 0;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 3, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        pnCenter.add(lblUsername, c);
        c.gridx = 1;
        c.gridy = 1;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 0, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        txtUsername.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtUsername, c);

        c.gridx = 0;
        c.gridy = 2;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 3, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        pnCenter.add(lblPassword, c);
        c.gridx = 1;
        c.gridy = 2;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 0, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        txtPassword.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(txtPassword, c);

        c.gridx = 0;
        c.gridy = 3;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 3, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        pnCenter.add(lblFunction, c);
        c.gridx = 1;
        c.gridy = 3;
        c.gridheight = 1;
        c.insets = new Insets(5, 3, 0, 3);
        c.anchor = GridBagConstraints.SOUTHWEST;
        cbFunction.addItem("Employee");
        cbFunction.addItem("Administrator");
        cbFunction.setPreferredSize(new Dimension(300, 30));
        pnCenter.add(cbFunction, c);

        pnButton.setLayout(new FlowLayout());
        pnButton.setBorder(BorderFactory.createEtchedBorder());
        pnButton.add(btnLogin);
        pnButton.add(btnReset);

        pnMain.setLayout(new BorderLayout());
        pnMain.setBackground(Color.LIGHT_GRAY);
        pnMain.setBorder(BorderFactory.createRaisedSoftBevelBorder());
        pnMain.add(pnTitle, BorderLayout.NORTH);
        pnMain.add(pnCenter, BorderLayout.CENTER);
        pnMain.add(pnButton, BorderLayout.SOUTH);

        getContentPane().add(pnMain, BorderLayout.CENTER);
    }

    private void registerListeners() {
        btnLogin.addActionListener(this);
        btnReset.addActionListener(this);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource().equals(btnLogin)) {
            login();
        } else if (e.getSource().equals(btnReset)) {
            txtPassword.setText("");
            txtUsername.setText("");
        }
    }

    private void login() {
        if (cbFunction.getSelectedIndex()==0) {
            ArrayList<EmployeeLogin> lst = lgDal.getLogin(txtUsername.getText());
            if (lst.size() == 0) {
                JOptionPane.showMessageDialog(pnCenter, "Login Fail! Username In correct!", "Login", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (lst.get(0).getPassword().equals(txtPassword.getText())) {
                JOptionPane.showMessageDialog(pnCenter, "Login Successful!", "Login", JOptionPane.INFORMATION_MESSAGE);
                int role = 0;
                String[] args = {Integer.toString(lst.get(0).getEmpId()), txtUsername.getText(), Integer.toString(role)};
                Utilities.empId = Integer.toString(lst.get(0).getEmpId());
                Utilities.username = txtUsername.getText();
                launch(CourierSystem.class, args);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(pnCenter, "Login Fail! Password Incorrect!", "Login", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        } else if(cbFunction.getSelectedIndex()==1){
            ArrayList<Administrator> lst = lgDal.getAdminLogin(txtUsername.getText());
            if (lst.size() == 0) {
                JOptionPane.showMessageDialog(pnCenter, "Login Fail! Username In correct!", "Login", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (lst.get(0).getAdPass().equals(txtPassword.getText())) {
                JOptionPane.showMessageDialog(pnCenter, "Login Successful!", "Login", JOptionPane.INFORMATION_MESSAGE);
                int role = Integer.parseInt(lst.get(0).getRole());
                String[] args = {Integer.toString(lst.get(0).getAdId()), txtUsername.getText(), lst.get(0).getRole()};
                Utilities.empId = Integer.toString(lst.get(0).getAdId());
                Utilities.username = txtUsername.getText();
                launch(CourierSystem.class, args);
                this.dispose();
            } else {
                JOptionPane.showMessageDialog(pnCenter, "Login Fail! Password Incorrect!", "Login", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
        }
    }

    public static void main(String[] args) {
        Login f = new Login();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        int height = screenSize.height;
        int width = screenSize.width;
        f.setSize(width / 2, height / 2);
        f.setLocationRelativeTo(null);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setSize(new Dimension(500, 300));
        f.setVisible(true);
    }
}
